﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class HUDAniSetting : MonoBehaviour
{
    /// <summary>
    /// 动画播放持续时间;
    /// </summary>
    public float m_fDurationTime = 2.0f;

    public List<HudAnimAttribute> hudAnimAttibutes;    // 配置列表

    public float m_fNumberScaleMin = 0.8f;
    public float m_fNumberScaleMax = 0.8f;
    public float CameraNearDist = 6.5f;
    public float CameraFarDist = 40.0f;
    
    public TextAsset AssetAll;
    public List<KeyValue<Texture>> TextureList;
    public List<KeyValue<Shader>> ShaderList;

    public bool bRefresh = false;
    
    public bool OpenTest = false;
    public EHUDRenderType TestHudNumType = EHUDRenderType.NORMAL_ATTACK;
    public int TestFrameCount = 30;
    public bool HUDVisible = true;

    private void Start ()
    {
        // 启动游戏场景后调用
        HUDMgr.Instance.Init(this);
    }
    
    private void Update()
    {
#if UNITY_EDITOR
        HUDMgr.Instance.HUDVisible = HUDVisible;
        
        if (bRefresh)
        {
            bRefresh = false;
            HUDMgr.Instance.ApplySetting(this);
        }
#endif
        
        HUDMgr.Instance.Update(Time.deltaTime);
        
#if UNITY_EDITOR
        if (!OpenTest || Time.frameCount % TestFrameCount != 0)
        {
            return;
        }
        int nNumber = UnityEngine.Random.Range(-99999, 99999);
        HUDMgr.Instance.AddHUDNumber(TestHudNumType, nNumber, transform.position, false, false, 1);
#endif
    }
    
    private void OnDestroy()
    {
        HUDMgr.Instance.Deinit();
    }
}

[Serializable]
public class KeyValue<T>
{
    [SerializeField]
    public string Key;
    [SerializeField]
    public T Value;
}

public enum HUDAlignType
{
    align_left,   // 左对齐
    align_center, // 右对齐
    align_right,  // 居中
};

[Serializable]
public struct HudAnimAttribute
{
    public EHUDRenderType HUDRenderType;
    public string szHeadName;
    public string szPrefix;
    public AnimationCurve AlphaCurve;
    public AnimationCurve ScaleCurve;
    public AnimationCurve MoveCurve;
    public float OffsetX;
    public float OffsetY;
    public float OffsetRangXMin;
    public float OffsetRangXMax;
    public float OffsetRangYMin;
    public float OffsetRangYMax;
    public int SpriteGap; // 图片间隔
    public HUDAlignType AlignType;
    public bool ScreenAlign; // 是不是按屏幕对齐
    public HUDAlignType ScreenAlignType; // 屏幕对齐类型
}

public class HUDSetting
{
    public float m_fDurationTime = 2.0f;

    public float m_fNumberScaleMin = 0.8f;
    public float m_fNumberScaleMax = 0.8f;
    public float CameraNearDist = 6.5f;
    public float CameraFarDist = 40.0f;

    public HudAnimAttribute []NumberAttibute; // HUD_SHOW_NUMBER
    
    public TextAsset AssetAll;
    public List<KeyValue<Texture>> TextureList;
    public List<KeyValue<Shader>> ShaderList;

    public void InitSetting(HUDAniSetting hudAniSetting)
    {
        NumberAttibute = new HudAnimAttribute[hudAniSetting.hudAnimAttibutes.Count];
        
        for(int c = 0; c < NumberAttibute.Length; c++)
        {
            NumberAttibute[c] = hudAniSetting.hudAnimAttibutes[c];
        }

        m_fDurationTime = hudAniSetting.m_fDurationTime;
        m_fNumberScaleMin = hudAniSetting.m_fNumberScaleMin;
        m_fNumberScaleMax = hudAniSetting.m_fNumberScaleMax;
        CameraNearDist = hudAniSetting.CameraNearDist;
        CameraFarDist = hudAniSetting.CameraFarDist;
        
        AssetAll = hudAniSetting.AssetAll;
        TextureList = hudAniSetting.TextureList;
        ShaderList = hudAniSetting.ShaderList;
    }

    public Texture GetTextureByPath(string path)
    {
        Texture texture = null;
        for (int i = 0; i < TextureList.Count; ++i)
        {
            if (TextureList[i].Key == path)
            {
                texture = TextureList[i].Value;
            }
        }

        return texture;
    }

    public Shader GetShaderByPath(string path)
    {
        Shader shader = null;
        for (int i = 0; i < ShaderList.Count; ++i)
        {
            if (ShaderList[i].Key == path)
            {
                shader = ShaderList[i].Value;
            }
        }

        return shader;
    }
}